Ext.onReady(function(){
    var current_order_id; //当前选择的order的id
    var current_ticket_id; //当前选择的ticket的id
    var current_order_status; //当前order的状态
    var current_ticket_status; //当前ticket的状态
    var ordersrow;
    var ticketsrow;
    setTimeout("refreshDS()", 300000); //5分钟
    refreshDS = function(){
        var time = parseInt(refreshTime.getValue()) * 60000;
        orderds.reload();
        ticketds.baseParams.id = "";
        ticketds.reload();
        setTimeout("refreshDS()", time);
    };
    var before = new Ext.form.Checkbox({
        fieldLabel: '以前的',
        hideLabel: true,
        boxLabel: '以前的',
        name: 'before',
        checked: false,
        anchor: '90%'
    });
    var created_d = new Ext.form.Radio({
        fieldLabel: '日期类型',
        hideLabel: true,
        name: 'date_type',
        boxLabel: '订单日期',
        checked: true,
        inputValue: 'created_at'
    });
    var fly_d = new Ext.form.Radio({
        fieldLabel: '',
        hideLabel: true,
        name: 'date_type',
        boxLabel: '乘机日期',
        checked: false,
        inputValue: ''
    });
    var ticket_out_d = new Ext.form.Radio({
        fieldLabel: '',
        hideLabel: true,
        name: 'date_type',
        boxLabel: '出票日期',
        checked: false,
        inputValue: 'ticket_out_date'
    });
    var date_field = new Ext.form.DateField({
        fieldLabel: '日期查询',
        name: 'date',
        altFormats: 'Y-m-d',
        format: 'Y-m-d', //格式化日期
        //disabledDays: [0,6],	//不让选择周六，周日
        //disabledDaysText: "周末要休息",
        invalidText: "您输入的日期格式不对，正确格式为2008-01-01",
        allowBlank: false,
        //emptyText: '请选择...',
        inputType: '2008-01-01',
        //anchor: '50%'
        width: 100,
        value: today
    });
    var refreshTime = new Ext.form.ComboBox({
        store: new Ext.data.Store({
            reader: new Ext.data.ArrayReader({}, ['value']),
            data: [['1'], ['5'], ['10'], ['30']]
        }),
        name: 're_time',
        fieldLabel: '刷新间隔(分)',
        resiable: true,
        typeAhead: true,
        width: 60,
        handleHeight: 100,
        displayField: 'value',
        valueField: 'value',
        nameField: 're_time',
        selectOnFocus: true,
        mode: 'local',
        selectOnFocus: true,
        triggerAction: 'all', //这个属性非常重要,可以在选择一个值以后,依然还显示其它的值.
        readOnly: true,
        allowBlank: true,
        value: '5'
    });
    
    //提交按钮
    var submitButton = new Ext.Button({
        text: '查询',
        handler: function(){
            if (formPanel.form.isValid()) {
                var date = date_field.getValue().format('Y-m-d');
                var date_type = "";
                if (created_d.checked) {
                    date_type = "created_at";
                }
                else if (fly_d.checked) {
                    date_type = "fly_date";
                }
                else if (ticket_out_d.checked) {
                    date_type = "ticket_out_date";
                }
                if (before.checked){
                    orderds.baseParams.before = "yes";
                }else{
                    orderds.baseParams.before = "no";
                }
                orderds.baseParams.date_type = date_type;
                orderds.baseParams.date = date;
                orderds.reload();
                ticketds.baseParams.id = "";
                ticketds.reload();
            }
            else {
                Ext.MessageBox.alert('提示信息', '请将必填项信息录入后再提交.');
            }
        }
    })
    
    var formPanel = new Ext.form.FormPanel({
        //baseCls: 'x-plain',
        labelWidth: 75,
        width: '100%',
        frame: true,
        title: '出保险',
        labelAlign: 'right',
        items: [{
            layout: 'column',
            border: false,
            items: [{
                columnWidth: .2,
                layout: 'form',
                border: false,
                items: [date_field]
            }, {
                columnWidth: .08,
                layout: 'form',
                border: false,
                items: [created_d]
            }, {
                columnWidth: .08,
                layout: 'form',
                border: false,
                items: [fly_d]
            }, {
                columnWidth: .08,
                layout: 'form',
                border: false,
                items: [ticket_out_d]
            }, {
                columnWidth: .18,
                layout: 'form',
                border: false,
                items: [refreshTime]
            }, {
                columnWidth: .1,
                labelWidth: 0,
                border: false,
                items: [before]
            }, {
                columnWidth: .2,
                layout: 'form',
                border: false,
                items: [submitButton]
            }]
        }]
    });
    
    formPanel.render('out_insurance_form');
    
    //############## 订单Grid ##############
    var ordercm = new Ext.grid.ColumnModel([new Ext.grid.RowNumberer(), {
        header: '编号',
        dataIndex: 'id',
        hidden: true
    }, {
        header: '订单编号',
        dataIndex: 'order_no',
        width: 120
    }, {
        header: 'PNR',
        dataIndex: 'pnr',
        width: 60
    }, {
        header: '客户名称',
        dataIndex: 'customer',
        width: 70
    }, {
        header: '订单状态',
        dataIndex: 'status',
        width: 60
    }, {
        header: '总金额',
        dataIndex: 'price_sum',
        width: 50
    }, {
        header: '保险份数',
        dataIndex: 'insurance_number',
        width: 60
    }, {
        header: '乘机日期',
        dataIndex: 'fly_date',
        width: 70
    //,
    //renderer: Ext.util.Format.dateRenderer('Y-m-d')
    }, {
        header: '起飞时间',
        dataIndex: 'fly_time',
        width: 60
    }, {
        header: '联系人',
        dataIndex: 'link_man',
        width: 60
    }, {
        header: '联系电话',
        dataIndex: 'link_tel',
        width: 90
    }, {
        header: '预定员',
        dataIndex: 'creator',
        width: 50
    }, {
        header: '预定(T)',
        dataIndex: 'created_at',
        width: 110,
        renderer: Ext.util.Format.dateRenderer('Y-m-d H:i')
    }, {
        header: '出票员',
        dataIndex: 'ticket_outer',
        width: 50
    }, {
        header: '出票(T)',
        dataIndex: 'ticket_out_date',
        width: 110,
        renderer: Ext.util.Format.dateRenderer('Y-m-d H:i')
    }, {
        header: '送票日期',
        dataIndex: 'send_date',
        width: 70,
        hidden: true
    }, {
        header: '送票地址',
        dataIndex: 'send_address',
        width: 130,
        hidden: true
    }, {
        header: '客户要求',
        dataIndex: 'order_request',
        width: 120,
        hidden: true
    }]);
    ordercm.defaultSortable = true;
    var orderds = new Ext.data.Store({
        proxy: new Ext.data.HttpProxy({
            url: '/orders/getInsuranceList'
        }),
        reader: orderReader
    });
    
    var ordergridpanel = new Ext.grid.GridPanel({
        el: 'ordersgrid',
        ds: orderds,
        cm: ordercm,
        loadMask: true,
        autoWidth: true,
        collapsible: true,
        animCollapse: false,
        height: 330,
        title: '定单信息'
    });
    
    ordergridpanel.render();
    orderds.load();
    
    //双击订单查看机票详细
    ordergridpanel.on('rowdblclick', function(ordergridpanel, rowIndex, e){
        var current_order_selected = orderds.data.items[rowIndex].id;
        ordersrow = orderds.getById(current_order_selected);//ds为绑定到grid的数据源
        current_order_id = ordersrow.get("id");
        current_order_status = ordersrow.get("status");
        ticketds.baseParams.id = current_order_id;
        ticketds.reload();
    });
    //订单右键菜单
    ordergridpanel.on('rowcontextmenu', function(customergridpanel, rowIndex, e){
        var current_order_selected = orderds.data.items[rowIndex].id;
        ordersrow = orderds.getById(current_order_selected);//ds为绑定到grid的数据源
        current_order_id = ordersrow.get("id");
        current_order_status = ordersrow.get("status");
        e.preventDefault();
        rightClick.showAt(e.getXY());
    });
    
    //############ 订单-右键菜单 ##########
    var rightClick = new Ext.menu.Menu({
        items: [{
            handler: rightMenu1Fn,
            text: '【提取PNR】'
        }]
    });
    function rightMenu1Fn(){
        showPnrInfo(current_order_id);
    }
    
    //### 机票Grid ###
    var ticketcm = new Ext.grid.ColumnModel([new Ext.grid.RowNumberer(), {
        header: '编号',
        dataIndex: 'id',
        hidden: true
    }, {
        header: 'PNR',
        dataIndex: 'pnr',
        width: 60
    }, {
        header: '旅客姓名',
        dataIndex: 'passenger_name',
        width: 70
    }, {
        header: '证件类型',
        dataIndex: 'card_type',
        width: 60
    }, {
        header: '证件号码',
        dataIndex: 'card',
        width: 140
    }, {
        header: '票号',
        dataIndex: 'ticket_no',
        width: 120
    }, {
        header: '状态',
        dataIndex: 'status_type',
        width: 50
    }, {
        header: '出票状态',
        dataIndex: 'status',
        width: 60
    }, {
        header: '保险份数',
        dataIndex: 'insurance_number',
        width: 60
    /*
         }, {
         header: '出保险人',
         dataIndex: 'insurance_outer',
         width: 70
             */
    }, {
        header: '出保险时间',
        dataIndex: 'insurance_out_date',
        width: 110,
        renderer: Ext.util.Format.dateRenderer('Y-m-d H:i')
    }, {
        header: '保单号',
        dataIndex: 'insurance_no',
        width: 100
    }, {
        header: '航班号',
        dataIndex: 'air_no',
        width: 60
    }, {
        header: '航程',
        dataIndex: 'fromto',
        width: 60
    }, {
        header: '乘机日期',
        dataIndex: 'fly_date',
        width: 70
    //,
    //renderer: Ext.util.Format.dateRenderer('Y-m-d')
    }, {
        header: '起飞时间',
        dataIndex: 'fly_time',
        width: 60
    }, {
        header: '舱位',
        dataIndex: 'position',
        width: 40
    }, {
        header: '机票价格',
        dataIndex: 'price',
        width: 60
    }, {
        header: '机建费',
        dataIndex: 'airdrome_tax',
        width: 50
    }, {
        header: '附加费',
        dataIndex: 'fuel_tax',
        width: 50
    }, {
        header: 'F',
        dataIndex: 'price_F',
        width: 40
    }, {
        header: '保险F',
        dataIndex: 'insurance_F',
        width: 50
    }, {
        header: '其他',
        dataIndex: 'other',
        width: 50
    }, {
        header: '积分',
        dataIndex: 'point',
        width: 50
    }, {
        header: '合计',
        dataIndex: 'total_price',
        width: 70
    
    }]);
    ticketcm.defaultSortable = true;
    var ticketds = new Ext.data.Store({
        proxy: new Ext.data.HttpProxy({
            url: '/orders/getInsuranceTickets'
        }),
        reader: new Ext.data.JsonReader({
            root: 'root'
        }, [{
            name: 'id'
        }, {
            name: 'pnr'
        }, {
            name: 'order_id'
        }, {
            name: 'customer_id'
        }, {
            name: 'passenger_id'
        }, {
            name: 'card_type',
            mapping: 'passenger.card_type.card_type'
        }, {
            name: 'passenger_name',
            mapping: 'passenger.name'
        }, {
            name: 'card',
            mapping: 'passenger.card'
        }, {
            name: 'ticket_no'
        }, {
            name: 'air_no'
        }, {
            name: 'fromto'
        }, {
            name: 'fly_date'
        }, {
            name: 'fly_time'
        }, {
            name: 'arr_time'
        }, {
            name: 'position'
        }, {
            name: 'status_type'
        }, {
            name: 'trip_type'
        }, {
            name: 'ticket_type'
        }, {
            name: 'price'
        }, {
            name: 'status'
        }, {
            name: 'airdrome_tax'
        }, {
            name: 'fuel_tax'
        }, {
            name: 'price_F'
        }, {
            name: 'insurance_number'
        }, {
            name: 'insurance_F'
        }, {
            name: 'other'
        }, {
            name: 'total_price'
        }, {
            name: 'insurance_campany'
        /*
             }, {
             name: 'insurance_outer',
             mapping: 'insurance_outer.user_name'
                 */
        }, {
            name: 'insurance_outer_id'
        }, {
            name: 'insurance_out_date'
        },{
            name: 'insurance_no'
        }, {
            name: 'canceler_id'
        }, {
            name: 'cancel_reason'
        }, {
            name: 'point'
        }, {
            name: 'rake_off'
        }, {
            name: 'policy_id'
        }, {
            name: 'father_id'
        }])
    });
    
    var ticketgridpanel = new Ext.grid.GridPanel({
        el: 'ticketsgrid',
        ds: ticketds,
        cm: ticketcm,
        loadMask: true,
        autoWidth: true,
        collapsible: true,
        animCollapse: false,
        height: 180,
        title: '航班信息'
    });
    
    ticketgridpanel.render();
    ticketds.load();
    
    var outInsuranceWindow;
    function ticketRightMenu1(){
        var i_num = ticketsrow.get("insurance_number");
        if (i_num > 0) {
            var ticket_id = ticketsrow.get("id");
            var passenger_name = ticketsrow.get("passenger_name");
            var card = ticketsrow.get("card");
            var card_type = ticketsrow.get("card_type");
            var fly_date = Ext.util.Format.dateRenderer('Y-m-d')(ticketsrow.get("fly_date"));
            var insurance_out_date = Ext.util.Format.dateRenderer('Y-m-d')(ticketsrow.get("insurance_out_date"));
            var url = "/orders/insuranceWindow/";
            url = url + "?fly_date="+fly_date+"&passenger_name=" + encodeURIComponent(passenger_name) + "&card=" + card + "&card_type=" + encodeURIComponent(card_type);
            //Ext.MessageBox.alert('提示', url);
            
            var closeAndSaveButton = new Ext.Button({
                text: '关闭并保存',
                handler: function(){
                    closeAndSaveButton.setDisabled(true);
                    Ext.Ajax.request({
                        url: '/orders/outInsurance',
                        params: 'id=' + current_ticket_id,
                        method: 'GET',
                        success: function(result, resp){
                            if (resp.result.info.indexOf("成功") >= 0) {
                                insuranceNo.setValue(resp.result.insurance_no);
                                //Ext.MessageBox.alert('提示', resp.result.info);
                                ticketds.reload();
                            }
                            else {
                                Ext.MessageBox.alert('操作失败', '' + resp.result.info);
                            //outInsuranceWindow.destroy();
                            }
                        },
                        failure: function(result, request){
                            Ext.MessageBox.alert('错误提示', '网络通信失败请检查网络或联系管理员!');
                            outInsuranceWindow.destroy();
                        }
                    })
                }
            })
            if (insurance_out_date!=""){
                closeAndSaveButton.setDisabled(true);
            }
            var closeInsuranceWinButton = new Ext.Button({
                text: '关闭',
                handler: function(){
                    outInsuranceWindow.destroy();
                }
            })
            
            var outInsuranceWindow = new Ext.Window({
                layout: 'fit',
                width: 800,
                height: 600,
                //closeAction: 'hide',
                plain: true,
                modal: true,
                title: '出保险-打印保险单',
                autoLoad: {
                    url: url,
                    scripts: true
                },
                //items:insuranceFormPanel,
                buttonAlign: 'center',
                buttons: [closeInsuranceWinButton,closeAndSaveButton]
            });
            outInsuranceWindow.show();
        }
        else {
            Ext.MessageBox.alert('提示', "这张票上没有保险记录");
        }
    }
    
    var ticketRightClick = new Ext.menu.Menu({
        items: [{
            handler: ticketRightMenu1,
            text: '【出保险】'
        }]
    });
    ticketgridpanel.on('rowcontextmenu', function(ticketgridpanel, rowIndex, e){
        var current_ticket_selected = ticketds.data.items[rowIndex].id;
        ticketsrow = ticketds.getById(current_ticket_selected);//ds为绑定到grid的数据源
        current_ticket_id = ticketsrow.get("id");
        current_ticket_status = ticketsrow.get("status");
        e.preventDefault();
        ticketRightClick.showAt(e.getXY());
    });
    //双击票
    ticketgridpanel.on('rowdblclick', function(ticketgridpanel, rowIndex, e){
        var current_ticket_selected = ticketds.data.items[rowIndex].id;
        ticketsrow = ticketds.getById(current_ticket_selected);//ds为绑定到grid的数据源
        current_ticket_id = ticketsrow.get("id");
        current_ticket_status = ticketsrow.get("status");
        
        ticketRightMenu1();
    });
})
